package org.devbg.academy.corejava.shop.ui.models;

import java.util.SortedSet;

import javax.swing.event.TreeExpansionEvent;
import javax.swing.event.TreeWillExpandListener;
import javax.swing.tree.ExpandVetoException;

import org.devbg.academy.corejava.shop.db.entities.Category;

public class CategoryExpandListener implements TreeWillExpandListener {
	public void treeWillCollapse(TreeExpansionEvent event)
			throws ExpandVetoException {

	}

	public void treeWillExpand(TreeExpansionEvent event)
			throws ExpandVetoException {
		Object lastElement = event.getPath().getLastPathComponent();
		if (lastElement instanceof CategoryTreeNode) {
			CategoryTreeNode node = (CategoryTreeNode) lastElement;
			if (node.getChildCount() == 0) {
				SortedSet<Category> childs = node.getCategory()
						.getChildCategories();
				for (Category category : childs) {
					node.add(new CategoryTreeNode(category));
				}
			}
		}
	}

}
